/*
 *  Copyright 2019-2023 oyun
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.oyun.modules.medicine.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oyun.modules.medicine.domain.Hospital;
import com.oyun.modules.medicine.domain.RenownedExpert;
import com.oyun.modules.medicine.domain.vo.RenownedExpertQueryCriteria;
import com.oyun.modules.medicine.mapper.HospitalMapper;
import com.oyun.modules.medicine.mapper.RenownedExpertMapper;
import com.oyun.modules.medicine.service.RenownedExpertService;
import com.oyun.segment.convert.itmd.IntermediateCodeUtils;
import com.oyun.utils.FileUtil;
import com.oyun.utils.PageResult;
import com.oyun.utils.PageUtil;
import com.oyun.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author oyun
 * @description 服务实现
 * @date 2024-04-30
 **/
@Service
@RequiredArgsConstructor
public class RenownedExpertServiceImpl extends ServiceImpl<RenownedExpertMapper, RenownedExpert> implements RenownedExpertService {

    private final RenownedExpertMapper renownedExpertMapper;

    private final HospitalMapper hospitalMapper;

    @Override
    public PageResult<RenownedExpert> queryAll(RenownedExpertQueryCriteria criteria, Page<Object> page) {
        if (StringUtils.isNotEmpty(criteria.getName())) {
            criteria.setReplaceAll(IntermediateCodeUtils.mn2Code(criteria.getName()).replaceAll("-", " -"));
        }
        IPage<RenownedExpert> all = renownedExpertMapper.findAll(criteria, page);
        Acquire(all.getRecords());
        return PageUtil.toPage(all);
    }

    @Override
    public List<RenownedExpert> queryAll(RenownedExpertQueryCriteria criteria) {
        return renownedExpertMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(RenownedExpert resources) {
        convertCode(resources);
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RenownedExpert resources) {
        RenownedExpert renownedExpert = getById(resources.getId());
        renownedExpert.copy(resources);
        convertCode(renownedExpert);
        saveOrUpdate(renownedExpert);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<RenownedExpert> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RenownedExpert renownedExpert : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put(" hpId", renownedExpert.getHpId());
            map.put("0 男 1女", renownedExpert.getSex());
            map.put("民族", renownedExpert.getNation());
            map.put("职称(字典)", renownedExpert.getProfessionalPost());
            map.put("专家类型(字典)", renownedExpert.getType());
            map.put("名字", renownedExpert.getMnName());
            map.put(" zhName", renownedExpert.getZhName());
            map.put(" mnNameCode", renownedExpert.getMnNameCode());
            map.put("生日", renownedExpert.getBirthday());
            map.put("去世日期", renownedExpert.getMnDateDeath());
            map.put("特长", renownedExpert.getMnStrongPoint());
            map.put(" zhStrongPoin", renownedExpert.getZhStrongPoin());
            map.put("简介", renownedExpert.getMnIntro());
            map.put(" zhIntro", renownedExpert.getZhIntro());
            map.put(" createBy", renownedExpert.getCreateBy());
            map.put(" createTime", renownedExpert.getCreateTime());
            map.put(" updateBy", renownedExpert.getUpdateBy());
            map.put(" updateTime", renownedExpert.getUpdateTime());
            map.put(" status", renownedExpert.getStatus());
            map.put("备注", renownedExpert.getRemark());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Object getByName(String name) {
        String replaceAll = IntermediateCodeUtils.mn2Code(name).replaceAll("-", " -");
        LambdaQueryWrapper<RenownedExpert> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(RenownedExpert::getZhName, name).or().like(RenownedExpert::getMnNameCode, replaceAll);
        List<RenownedExpert> list = renownedExpertMapper.selectList(wrapper);
        Acquire(list);
        return list;
    }

    private void convertCode(RenownedExpert renownedExpert) {
        renownedExpert.setMnNameCode(IntermediateCodeUtils.mn2Code(renownedExpert.getMnName()).replaceAll("-", " -"));
    }

    private void Acquire(List<RenownedExpert> renownedExperts) {
        for (RenownedExpert renownedExpert : renownedExperts) {
            LambdaQueryWrapper<Hospital> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(renownedExpert.getHpId() != null, Hospital::getId, renownedExpert.getHpId());
            Hospital hospital = hospitalMapper.selectOne(wrapper);
            if (hospital != null) renownedExpert.setHpName(hospital.getMnName());
        }
    }
}
